//
//  String+attributedText.swift
//  MobileRTCSwiftSample
//
//  Created by zhangheng on 2019/5/29.
//  Copyright © 2019 Zoom Video Communications, Inc. All rights reserved.
//

import Foundation

extension String {
    
    /// range转换为NSRange
    func nsRange(from range: Range<String.Index>) -> NSRange {
        return NSRange(range, in: self)
    }
    
    /// NSRange转化为range
    func range(from nsRange: NSRange) -> Range<String.Index>? {
        guard
            let from16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location, limitedBy: utf16.endIndex),
            let to16 = utf16.index(from16, offsetBy: nsRange.length, limitedBy: utf16.endIndex),
            let from = String.Index(from16, within: self),
            let to = String.Index(to16, within: self)
            else { return nil }
        return from ..< to
    }
}

extension String {
    
    func attributedText(_ attributString:String) -> NSMutableAttributedString {
        let attrStr = NSMutableAttributedString.init(string: self)
        //attrStr.addAttribute(NSAttributedStringKey.foregroundColor, value:UIColor.orange, range:NSRange.init(location:0, length: 8))
        let shadow = NSShadow.init()
        shadow.shadowColor = NSColor.red
        shadow.shadowOffset = CGSize.init(width: 2, height: 2)
        
        let range = self.range(of: attributString)
        if range != nil{
            let nsRange = self.nsRange(from: range!)
            let attributedstrings = [
                //NSAttributedStringKey.foregroundColor:UIColor.purple,                      //颜色
                NSAttributedString.Key.font:NSFont.boldSystemFont(ofSize: 14),               //仅字体和大小
                //NSAttributedStringKey.underlineStyle:NSUnderlineStyle.styleSingle.rawValue,//下划线
                //NSAttributedStringKey.shadow: shadow,                                      //阴影
                //NSAttributedStringKey.backgroundColor:UIColor.green,                       //背景色
                ] as [NSAttributedString.Key : Any]
            attrStr.addAttributes(attributedstrings, range: nsRange)
            return attrStr
        }
        return NSMutableAttributedString.init(string: self)
    }
    
    func attributedUnderline(text:String,link:URL?=nil,textColor:NSColor?=nil) -> NSMutableAttributedString {
        let attrStr = NSMutableAttributedString.init(string: self)
        let shadow = NSShadow.init()
        shadow.shadowColor = NSColor.red
        shadow.shadowOffset = CGSize.init(width: 2, height: 2)
        
        let range = self.range(of: text)
        if range != nil{
            let nsRange = self.nsRange(from: range!)
            attrStr.beginEditing()
            if link != nil {
                attrStr.addAttribute(.link, value: link!, range: nsRange)
            }
            let color = (textColor != nil) ? textColor:NSColor.blue
            attrStr.addAttribute(NSAttributedString.Key.underlineColor, value:color!, range:nsRange)
            attrStr.addAttribute(NSAttributedString.Key.foregroundColor, value:color!, range:nsRange)
            attrStr.endEditing()
            return attrStr
        }
        return NSMutableAttributedString.init(string: self)
    }
    
    func attributedT(text:String,textColor:NSColor,font:NSFont) -> NSMutableAttributedString {
        let attrStr = NSMutableAttributedString.init(string: self)
        
        let range = self.range(of: text)
        if range != nil{
            let nsRange = self.nsRange(from: range!)
            let attributedstrings = [
                NSAttributedString.Key.foregroundColor:textColor,                                 //颜色
                NSAttributedString.Key.font:font,
                ] as [NSAttributedString.Key : Any]
            attrStr.addAttributes(attributedstrings, range: nsRange)
            return attrStr
        }
        return attrStr
    }
    
  
}

extension String {
    ///////////特殊处理
      func subBracketsAttributed(textColor:NSColor,font:NSFont) -> NSMutableAttributedString {
          if self.contains(find: "（") && self.contains(find: "）"){
             let posintstart = self.positionOf(sub: "（")
             let posintend = self.positionOf(sub: "）")
             let subS = self.subString(start: posintstart, length: (posintend-posintstart+1))
              return self.attributedT(text: subS, textColor: textColor, font: font)
          }
          return NSMutableAttributedString.init(string: self)
      }
      
      func subColonAttributed(textColor:NSColor,font:NSFont) -> NSMutableAttributedString {
          if self.contains(find: "：") {
              let posintstart = self.positionOf(sub: "：")
              let subS = self.substring(fromIndex: posintstart+1)
              return self.attributedT(text: subS, textColor: textColor, font: font)
          }
          return NSMutableAttributedString.init(string: self)
      }
}
